package com.admin.service;

import com.admin.mapper.SensitiveWordsMapper;
import com.admin.model.SensitiveWords;
import com.admin.model.ServiceInstance;
import com.admin.model.ServiceRegistryState;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SensitiveWordsServiceImpl implements SensitiveWordsService{

    private static final Logger logger = LoggerFactory.getLogger(SensitiveWordsService.class);

    @Resource
    private SensitiveWordsMapper sensitiveWordsMapper;

    @Resource
    private NacosServiceManager nacosServiceManager;

    @Resource
    private EurekaServiceManager eurekaServiceManager;

    @Override
    public void insertSensitiveWord(SensitiveWords sensitiveWords) throws NacosException {
        logger.info("添加敏感词：{}", sensitiveWords.getWord());
        sensitiveWordsMapper.insert(sensitiveWords);
        //删除敏感词和新增敏感词都需要给客户端进行同步
        // 判断是否启用了Nacos
        if (ServiceRegistryState.isNacosEnabled()) {
            // 删除敏感词和新增敏感词都需要给客户端进行同步
            syncSensitiveWordsWithServices(sensitiveWords);
        }
        // 判断是否启用了Eureka
        if (ServiceRegistryState.isEurekaEnabled()) {
            // 删除敏感词和新增敏感词都需要给客户端进行同步
            syncSensitiveWordsWithEurekaServices(sensitiveWords);
        }
        //这些执行完了之后返回就行了，如果出现错误需要提交错误报告
    }

    @Override
    public void deleteSensitiveWordById(SensitiveWords sensitiveWords) throws NacosException {
        logger.info("删除敏感词，ID：{}", sensitiveWords.getId());
        sensitiveWordsMapper.deleteById(sensitiveWords.getId());
        //删除敏感词和新增敏感词都需要给客户端进行同步
        // 判断是否启用了Nacos
        if (ServiceRegistryState.isNacosEnabled()) {
            // 删除敏感词和新增敏感词都需要给客户端进行同步
            syncSensitiveWordsWithServices(sensitiveWords);
        }
        // 判断是否启用了Eureka
        if (ServiceRegistryState.isEurekaEnabled()) {
            // 删除敏感词和新增敏感词都需要给客户端进行同步
            syncSensitiveWordsWithEurekaServices(sensitiveWords);
        }
    }

    private void syncSensitiveWordsWithServices(SensitiveWords sensitiveWords) throws NacosException {
        List<ServiceInfo> servicesByGroup = nacosServiceManager.getServicesByGroup();
        if (servicesByGroup != null && !servicesByGroup.isEmpty()) {
            nacosServiceManager.performActionWithServiceInfo(servicesByGroup, sensitiveWords);
            logger.info("完成分组服务同步操作。");
        } else {
            logger.info("没有找到任何分组服务，跳过同步操作。");
        }

        List<ServiceInfo> servicesByNameList = nacosServiceManager.getServicesByNameList();
        if (servicesByNameList != null && !servicesByNameList.isEmpty()) {
            nacosServiceManager.performActionWithServiceInfo(servicesByNameList, sensitiveWords);
            logger.info("完成名称服务同步操作。");
        } else {
            logger.info("没有找到任何名称服务，跳过同步操作。");
        }
    }

    public void syncSensitiveWordsWithEurekaServices(SensitiveWords sensitiveWords) {
        // 通过服务名称同步
        List<ServiceInstance> serviceInstancesByServiceName = eurekaServiceManager.getServiceInstancesByServiceName();
        if (!serviceInstancesByServiceName.isEmpty()) {
            eurekaServiceManager.performActionWithServiceInfo(serviceInstancesByServiceName, sensitiveWords);
            logger.info("完成服务名称同步操作。");
        } else {
            logger.info("没有找到任何服务实例，跳过同步操作。");
        }

        // 通过分组名称同步
        List<ServiceInstance> serviceInstancesByGroupName = eurekaServiceManager.getServiceInstancesByGroupName();
        if (!serviceInstancesByGroupName.isEmpty()) {
            eurekaServiceManager.performActionWithServiceInfo(serviceInstancesByGroupName, sensitiveWords);
            logger.info("完成分组服务同步操作。");
        } else {
            logger.info("没有找到任何分组服务，跳过同步操作。");
        }
    }


    @Override
    public PageInfo<SensitiveWords> selectSensitiveWordsByPage(int pageNum, int pageSize) {
        logger.info("分页查询敏感词，页码：{}，每页数量：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<SensitiveWords> list = sensitiveWordsMapper.selectByPage(pageNum, pageSize);
        return new PageInfo<>(list);
    }
}
